<?php

    /**
    * 服务器脚本输出分页html
    * 
    * @author dujianfeng
    * @author Qingzhang Su
    * @package PageFormat
    */
    class PageFormat
    {
        public static $First = "首页";
        public static $Prev = "上一页";
        public static $Next = "下一页";
        public static $Last = "尾页";
        public static $PageCount = 0;      //页数
        public static $padLeft = 0;
        public static $padRight = 0;
        public static $CurrentPage = 0;    //当前页
        public static $RecordCount = 0;    //总记录数
        public static $DisplayPages = 0;   //显示页数
        public static $PageSize = 0;	   //每页显示记录数
        public static $Url = "";		   //跳转地址
        public static $extName = "php";	   //$Url不为空时使用，主要针对动态页, 使用url后参数防暑
        public static $urlParam = "";      //$Url不为空时使用，主要针对动态页，用于url的参数传递 (如： &param2=value2&param3&value3)

        /**
        * 分页输出到模板页
        * 
        * @access public
        * @param mixed $CurrentPage
        * @param mixed $RecordCount
        * @param mixed $DisplayPages
        * @param mixed $PageSize
        * @param mixed $Url
        * @param mixed $PageCount
        * @return void |string
        */
        public static function write($CurrentPage, $RecordCount, $DisplayPages, $PageSize, $Url = null, $PageCount = null){

            self::$CurrentPage = $CurrentPage;
            self::$RecordCount = $RecordCount;
            self::$DisplayPages = $DisplayPages;
            self::$PageSize = $PageSize;
            if($Url){
                self::$Url = $Url;
            }
            if($PageCount != null){
                self::$PageCount = $PageCount;
            } else {
                self::$PageCount = ceil($RecordCount / $PageSize);
            }
            self::$padLeft = floor($DisplayPages / 2);
            self::$padRight = self::$padLeft;

            if (self::$DisplayPages % 2 == 0)	self::$padLeft--;

            $Html = "";

            //if(($RecordCount > $PageSize) && ($CurrentPage <= self::$PageCount)){
            if($CurrentPage <= self::$PageCount){
                //if((($RecordCount > $PageSize) && ($CurrentPage <= self::$PageCount)) || (($PageCount >1) && ($CurrentPage <= self::$PageCount))){
                echo '<span id="mypage">';
                echo self::RenderBeginTag();
                echo self::RenderPagingContents();
                echo self::RenderEndTag();
                echo '</span>';
            }
        }

        public static function RenderBeginTag(){
            $str = "";
            if (self::$CurrentPage > 1) {
                $temp = (self::$CurrentPage - 1 == 1 )? "1" : (self::$CurrentPage - 1);

                if (self::$Url != "") {
                    $first = self::$Url.'1.html';
                    $preUrl = self::$Url.$temp.".html";
                    if(self::$extName == 'php'){
                        $first = self::$Url."p=1".self::$urlParam;
                        $preUrl = self::$Url."p=".$temp.self::$urlParam;
                    }

                    $str .= "<a id=\"page_first\"  href = \"" . $first.  "\" title=\"首页\" class=\"direct btnPage\">".self::$First."</a>";
                    $str .= "<span class=\"PrePageSpan\"><a id=\"page_pre_$temp\" href = \"" . $preUrl. "\" title=\"上一页\"  class=\"direct btnPage back\">" . self::$Prev . "</a></span>";
                }
            }  else {
                $str .= "<a style='visibility:hidden' id=\"page_first\"  href = \"" . self::$Url .  "1.html\" title=\"首页\" class=\"direct btnPage\">".self::$First."</a>";
                $str .= "<span style='visibility:hidden' class=\"PrePageSpan\"><a id=\"page_pr_e\" href = \"#\" title=\"上一页\"  class=\"direct btnPage back\">" . self::$Prev . "</a></span>";
            }
            return $str;
        }

        public static function RenderEndTag(){
            $str = "";
            $addition = "";

            if (self::$CurrentPage >= 1 && self::$CurrentPage != self::$RecordCount) {
                $temp = self::$CurrentPage + 1;
                $temp1 = self::$PageCount;
                if ($temp <= $temp1) {
                    if (self::$Url != "") {
                        $first = self::$Url.$temp.'.html';
                        $preUrl = self::$Url.$temp1.".html";
                        if(self::$extName == 'php'){
                            $first = self::$Url."p=".$temp.self::$urlParam;
                            $preUrl = self::$Url."p=".$temp1.self::$urlParam;
                        }

                        //$str .= self::$CurrentPage < self::$PageCount - 2 ? ".." : "";

                        $str .= "<span class=\"NextPageSpan\"><a id=\"page_next_$temp\" href = \"" . $first . "\" title=\"下一页\"  class=\"direct btnPage\"  >" . self::$Next . "</a></span>";
                        if (self::$CurrentPage < self::$PageCount - 1 && self::$CurrentPage + self::$padRight != self::$PageCount) {
                            $str .= "<a  id=\"page_last_$temp1\" href = \"" . $preUrl . "\" title=\"尾页\" class=\"direct btnPage\">".self::$Last."</a>";
                        } else {
                            $str .= "<a style='visibility:hidden'  id=\"page_last_\" href = \"#\" title=\"尾页\" class=\"direct btnPage\">".self::$Last."</a>";
                        }
                    }
                } else {
                    $str .= "<span style='visibility:hidden' class=\"NextPageSpan\"><a id=\"page_next_$temp\" href = \"#\" title=\"下一页\"  class=\"direct btnPage\"  >" . self::$Next . "</a></span>";
                    $str .= "<a style='visibility:hidden'  id=\"page_last_\" href = \"" . self::$Url . $temp1 . ".html\" title=\"尾页\" class=\"direct btnPage\">".self::$Last."</a>";	
                }
            } else {
                $temp = self::$CurrentPage + 1;
                $temp1 = self::$PageCount;

                $first = self::$Url.$temp.'.html';
                $preUrl = self::$Url.$temp1.".html";
                if(self::$extName == 'php'){
                    $first = self::$Url."p=".$temp.self::$urlParam;
                    $preUrl = self::$Url."p=".$temp1.self::$urlParam;
                }

                $str .= "<span style='visibility:hidden' class=\"NextPageSpan\"><a id=\"page_next_$temp\" href = \"#\" title=\"下一页\"  class=\"direct btnPage\"  >" . self::$Next . "</a></span>";
                $str .= "<a style='visibility:hidden'  id=\"page_last_\" href = \"" . $preUrl . "\" title=\"尾页\" class=\"direct btnPage\">".self::$Last."</a>";
            }
            return $str;
        }

        public static function RenderPagingContents(){
            $str = "";
            if (self::$PageCount <= self::$DisplayPages) {
                $str .= self::RenderLinkRange(1, self::$PageCount);

            }
            else {
                if (self::$CurrentPage <= self::$padLeft) {

                    $str .= self::RenderLinkRange(1, self::$DisplayPages);
                }
                else if (self::$CurrentPage <= (self::$PageCount - self::$padRight)) {
                    //$str .= "<span>".self::$CurrentPage."-".self::$padRight."</span>";
                    $str .= self::RenderLinkRange((self::$CurrentPage - self::$padLeft), (self::$CurrentPage + self::$padRight));

                }
                else {
                    $str .= self::RenderLinkRange((self::$CurrentPage - (self::$DisplayPages - ((self::$PageCount - self::$CurrentPage) + 1))), self::$PageCount);

                }
            }

            return $str;

        }

        public static function RenderLinkRange($start, $end){
            //$str = "<span>".$start."--".$end."</span>";
            $str = "";

            for ($i = $start; $i <= $end; $i++) {
                if ($i == self::$CurrentPage) {
                    $str .= "<span id=\"page_$i\" class=\"current\">$i</span>";
                }
                else {
                    $temp = ($i == 1 ? 1 : $i);

                    if (self::$Url != "") {
                        $curUrl = self::$Url.$temp.".html";
                        if(self::$extName == 'php'){
                            $curUrl = self::$Url."p=".$temp.self::$urlParam;
                        }

                        $str .= "<a  id=\"page_$temp\" href = \"" . $curUrl . "\" >$i</a>";
                    }
                }
            }
            return $str;
        }

        /**
        * 响应输出
        */
        public static function send($status = false, $message = null, $url = null, $isAjax=false,$isTop = false)
        {
            if ($isAjax) {
                return self::json($status, $message);
            }

            if (null === $url) {
                echo $message;
            } else {
                if($isTop)
                {
                    echo '<script type="text/javascript">alert("' . $message . '");top.location.href="' . $url . '";</script>';
                }
                else
                {
                    echo '<script type="text/javascript">alert("' . $message . '");location.href="' . $url . '";</script>';
                }
            }
            exit();
        }

        /**
        * 处理Json输出
        *
        * @param boolean $status 操作是否成功
        * @param mixed   $message  附加参数
        * @param mixed   $data
        */
        public static function json($status = false, $message = null, $data = null)
        {
            $response = array(
                'success' => $status,
                'message' => $message,
                'data' => $data
            );
            header('Content-Type: application/x-javascript; charset=utf-8');
            echo json_encode($response);
            exit();

        }
    }

    /** End of file Pager.class.php */